package com.integration.rest.client;

import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.integration.pojo.FileDTO;
import com.integration.pojo.TwittMessage;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

public class RestClient {
	private String host;
	private String port;
	private String contextRoot;
	


	public InputStream getFile(String sharing, String fileId, Integer index, Integer value) {

		Client client = new Client();
		WebResource resource = client.resource(String.format("http://%s:%s/%s", host, port, contextRoot));
		String path = String.format("/%s/%s", sharing, fileId);
		buildPath(resource, index, value);
		InputStream inptStream = resource.path(path).type(MediaType.APPLICATION_OCTET_STREAM_TYPE).get(InputStream.class);

		return inptStream;
	}
	
	public FileDTO getItem(String sharing, String fileId) throws JSONException {

		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		clientConfig.getClasses().add(JacksonJsonProvider.class);
		Client client = Client.create(clientConfig);
		String appURL = String.format("http://%s:%s/%s", host, port, contextRoot);
		WebResource resource = client.resource(appURL);
		String path = String.format("/%s/%s", sharing,fileId);
		String resp = resource.path(path).type(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		JSONObject obj = new JSONObject(resp);
		System.out.println(obj);
		
		return new FileDTO(obj);
	}
	
	public List<TwittMessage> getListMsgs() throws JSONException, ParseException {

		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		clientConfig.getClasses().add(JacksonJsonProvider.class);
		Client client = Client.create(clientConfig);
		String appURL = "http://knap-sack.appspot.com";
		WebResource resource = client.resource(appURL);
		String resp = resource.path("/TWTimeline").queryParam("id","robertafofonka").type(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		JSONArray arr = new JSONArray(resp);
		List<TwittMessage> msgs = new ArrayList<TwittMessage>();
		int length = arr.length();
		for (int i = 0; i < length; i++){
			msgs.add(new TwittMessage(arr.getJSONObject(i)));
		}
		
		return msgs;
	}
	
	
	public List<FileDTO> getList(String sharing) throws JSONException {

		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
		clientConfig.getClasses().add(JacksonJsonProvider.class);
		Client client = Client.create(clientConfig);
		String appURL = String.format("http://%s:%s/%s", host, port, contextRoot);
		WebResource resource = client.resource(appURL);
		String path = String.format("/%s", sharing);
		String payloadHTTP = resource.path(path).type(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		JSONArray receivedJSONArray = new JSONArray(payloadHTTP);
		List<FileDTO> resultList = new ArrayList<FileDTO>();
		for (int i = 0; i < receivedJSONArray.length(); i++){
			resultList.add(new FileDTO(receivedJSONArray.getJSONObject(i)));
		}
		System.out.println(receivedJSONArray);
		
		return resultList;
	}
	

	private void buildPath(WebResource resource, Integer index, Integer value) {
		if ((index != null && value != null) && (index > 0 && value > 0)) {
			UriBuilder uriBuilder = resource.getUriBuilder();
			Map<String, Integer> queryParameters = new HashMap<String, Integer>();
			queryParameters.put("index", index);
			queryParameters.put("value", value);
			uriBuilder.buildFromMap(queryParameters);
		}
	}
	

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getContextRoot() {
		return contextRoot;
	}

	public void setContextRoot(String contextRoot) {
		this.contextRoot = contextRoot;
	}
}
